import datetime

from flask import Flask, request, Blueprint, jsonify
from flask_restful import reqparse, Api
from db import db

user_blue = Blueprint('user_blue', __name__)
api = Api(user_blue)


@user_blue.route('Shipment')
def Shipment():
    dep_data = datetime.datetime.now()
    go_place = request.args.get('go_place')
    destination = request.args.get('destination')
    tool_id = request.args.get('tool_id')
    sql = 'select * from staff_model where status=0'
    res = db.find_all(sql)
    if res:
            try:
                sql1 = "insert into shipment(dep_data, go_place, destination,go_mileage,driver,details_is) \
                values ('%s','%s','%s','%s','%s','%s')" % (dep_data, go_place, destination, int(res[0]['sid'], int(tool_id)))

                db.update(sql1)

                sql2 = "update toll_admin set status=1 where tid=%s" % int(tool_id)
                db.update(sql2)
                db.commit()
            except Exception as e:
                print(e)
                return jsonify("报错了")
    else:
        return jsonify({"code": 200, "msg": "没人了"})

